package com.suanfa.string;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author jukg
 * @date 2025/01/09 11:37
 * @description
 * 计算字符串中最长的元音子字符串的长度
 *      * 元音字符包括：a, e, i, o, u（不区分大小写）
 *      * 子字符串被定义为字符串中连续的字符序列
 */
public class LongestVowelSubstringWithFlaw {
    public static void main(String[] args) {
        String s = "aeueo";
        int flaw = 2;
        System.out.println(longestVowelSubstring(flaw, s));
    }



    public static int longestVowelSubstring(int flaw, String s) {
        Set<Character> vowels = new HashSet<>();
        for (char c : "aeiouAEIOU".toCharArray()) {
            vowels.add(c);
        }
        int n = s.length();
        List<Integer> list = new ArrayList<>();
        for(int i=0; i< n; i++){
            if(vowels.contains(s.charAt(i))){
                list.add(i);
            }
        }

        int left = 0, right = 0;
        int maxLength = 0;


        while(right < list.size()){
            //计算瑕疵字符串长度，  根据下标减完之后还需要去掉中间的元音字母数量（right- left）
            int lenth = list.get(right) - list.get(left) - (right -left);

            if(lenth > flaw){
                left ++;
            }else{
                if(lenth == flaw){
                    //记录子串长度
                    int  slen = list.get(right) - list.get(left) +1;
                    maxLength = Math.max(maxLength, slen);
                }
                right ++;
            }


        }
        return maxLength;
    }
}
